package com.example.springboottest.student.service.impl;

import com.example.springboottest.student.dao.StuMongoJpaDao;
import com.example.springboottest.student.model.Student;
import com.example.springboottest.student.service.StuMogonService;
import com.example.springboottest.student.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 本来应该用StudentDocument对象，但是实际用Student一样有效
 * 但是回滚无法实现，不知道为什么
 */
@Service("stuMogonService")
public class StuMogonServiceImpl implements StuMogonService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private StuMongoJpaDao stuMongoJpaDao;

    @Override
    public List<Student> doFindALl() {
        return null;
    }

    @Override
    public void doDeleteById(String id) {
        Criteria criteriaid = Criteria.where("id").is(id);
        mongoTemplate.findAndRemove(Query.query(criteriaid), Student.class);
    }

    @Override
    public void doCreate(Student student) {
        mongoTemplate.save(student);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void doUpdate(Student student) {
        mongoTemplate.save(student);
//        stuMongoJpaDao.save(student);
//        int i = 9 / 0;
    }

    @Override
    public void idoUpdate(Student student) {
    }

    @Override
    public Student doFindById(String id) {
        return mongoTemplate.findById(id, Student.class);
        //如果只需要获取第一个，也可以采用如下查询方法
//         Criteria criteriaid =Criteria.where("id").is(id);
//         Query queryid = Query.query(criteriaid);
        //return mongoTemplate.findOne(queryid, Student.class) ;
    }

    @Override
    public Page<Student> doPage() {
        return null;
    }

    @Override
    public String doLock() {
        return "stop";
    }

    @Override
    public void doUpdateLock(Student student) {

    }
}